package com.luych.toolbox.common.service.security;

import com.luych.toolbox.common.feign.view.base.service.UserView;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    public static UserView getSession() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        if (securityContext == null) return null;
        Authentication authentication = securityContext.getAuthentication();
        if (authentication == null) return null;
        if (!authentication.isAuthenticated()) return null;
        Object principal = authentication.getPrincipal();
        if (principal == null) return null;
        if (!(principal instanceof UserView)) return null;
        return (UserView) principal;
    }
}
